package com.jt.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jt.entity.PointScadaConfig;
import com.jt.mapper.PointScadaConfigMapper;
import com.jt.service.PointScadaConfigService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class PointScadaConfigServiceImpl extends ServiceImpl<PointScadaConfigMapper, PointScadaConfig> implements PointScadaConfigService {
    @Override
    public void pointScadaConfigImport(MultipartFile file) {
        try {
            Workbook workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheetAt = workbook.getSheetAt(0);
            int lastRowNum = sheetAt.getLastRowNum();
            List<PointScadaConfig> resultList = new ArrayList<>();
            for (int i = 1; i < lastRowNum; i++) {
                PointScadaConfig pointScadaConfig = new PointScadaConfig();/*每行的数据*/
                Row row = sheetAt.getRow(i);
                short lastCellNum = row.getLastCellNum();
                for (int j = 0; j < lastCellNum; j++) {
                    Cell cell = row.getCell(j);
                    cell.setCellType(CellType.STRING);
                    String stringCellValue = cell.getStringCellValue();
                    if (stringCellValue != null && stringCellValue.length() != 0) {
                        switch (j) {
                            case 0:/**/
                                break;
                            case 1:/*检测点位名称*/
                                pointScadaConfig.setPointName(stringCellValue);
                                break;
                            case 2:/*是否报警点位*/
                                pointScadaConfig.setIsAlarm(stringCellValue);
                                break;
                            case 3:/*报警级别*/
                                pointScadaConfig.setAlarmLevel(stringCellValue);
                                break;
                            case 4:/*模型位号*/
                                pointScadaConfig.setModelPoint(stringCellValue);
                                break;
                            case 5:/*图纸和模型对照*/
                                break;
                            case 6:/*监测类型*/
                                pointScadaConfig.setPointClassId(stringCellValue);
                                break;
                            case 7:/*是否默认展示*/
                                pointScadaConfig.setIsShow(stringCellValue);
                                break;
                            case 8:/*点位*/
                                pointScadaConfig.setScadaPointCode(stringCellValue);
                                break;
                        }
                    }
                }
                resultList.add(pointScadaConfig);
            }
            this.saveBatch(resultList);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
